<?php

namespace App\Http\Controllers\Wx;

use App\Http\Controllers\Controller;
use App\Models\PlayerRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PlayerController extends Controller
{

    //
    public function rank(Request $request)
    {
        $id = $request->query('id', Auth::id());

        return PlayerRecord::query()
                           ->with('player')
                           ->where('player_id', $id)
                           ->orderBy('record_id', 'desc')
                           ->get();
    }

    public function statistics(Request $request)
    {
        $id = $request->query('id', Auth::id());

        $field = 'count(*) AS total,sum( score ) AS score, ';

        $field .= 'count( CASE WHEN score = 2 THEN 0 END ) mvp,';

        $field .= 'round( count( CASE WHEN score <> 0 THEN 0 END ) * 100 / count(*), 2 ) AS rate';

        return PlayerRecord::query()
                           ->selectRaw($field)
                           ->where('player_id', $id)
                           ->first();
    }
}
